<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * @author Меркулов Илья aka Bit0reZ
 * @license GNU GPLv2
 */

class Gadget extends CI_Controller {
	
        var $base_url;
	var $env = ENVIRONMENT;

	function __construct()
	{
		parent::__construct();
                $this->load->helper('url');
                $this->load->database('default');
                $this->load->library('parser');
                $this->load->library('session');
                $this->base_url = base_url();
	}

        function index()
        {
                show_404();
        }

        function tpl($name)
        {
                $post = $this->input->post('json');
                if($post) $data = json_decode($post, TRUE);
                else $data = array();
		if(isset($data['model'])){
			$data[$data['model']] = $this->wdb->$data['model']();
			unset($data['model']);
		}
                $data['base_url'] = base_url();
                $this->parser->parse('templates/'.$name.'.html', $data);
        }
	
	function jload($name)
	{
		header('Content-Type:text/javascript');
		$data['base_url'] = base_url();
                $this->parser->parse('js/'.$name.'.js', $data);
	}

	function gdata($name)
	{
		$param = $this->input->post('param');
		try{
			$this->load->library('gadgets/'.$name,'','gadget');
			$this->gadget->init($param);
			$gadget = $this->gadget->data();
			$this->output->set_output($gadget);
		}
		catch(Exception $e){ $this->output->set_output(json_encode(array('error' => 'Can`t load '.$name.'!'))); }
	}

        function gload($name)
	{
		$param = $this->input->post('param');
		try{
			$this->load->library('gadgets/'.$name,'','gadget');
			$this->gadget->init($param);
			$gadget = $this->gadget->load();
			$this->output->set_output($gadget);
		}
		catch(Exception $e){ $this->output->set_output(json_encode(array('error' => 'Can`t load '.$name.'!'))); }
        }
}
